/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import fs from 'fs';

import { FileUtil } from '../utils/file-util.js';

import { BaseCommandBuilder } from './base-command-builder.js';

/**
 * ohos native代码编译命令行构造类
 *
 * @since 2022/1/21
 */
export class NativeCommandBuilder extends BaseCommandBuilder {
  constructor(executor: string) {
    super([]);
    this.commandList.push(executor);
  }

  addCmakeList(cmakeList: string): NativeCommandBuilder {
    FileUtil.checkPathLength(cmakeList);
    this.commandList.push(`-H${cmakeList}`);
    return this;
  }

  addTempFilePath(tempFilePath: string): NativeCommandBuilder {
    FileUtil.checkPathLength(tempFilePath);
    this.commandList.push(`-B${tempFilePath}`);
    return this;
  }

  addOhosArch(arch: string): NativeCommandBuilder {
    this.commandList.push(`-DOHOS_ARCH=${arch}`);
    return this;
  }

  /**
   * 不要删除宏 CMAKE_LIBRARY_OUTPUT_DIRECTORY
   * 稳定作为cmake可用环境变量
   *
   * @param outputDir 库输出目录
   */
  addOutputDir(outputDir: string): NativeCommandBuilder {
    FileUtil.checkPathLength(outputDir);
    this.commandList.push(`-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${outputDir}`);
    return this;
  }

  addCMakeFindPath(findPath: string) {
    FileUtil.checkPathLength(findPath);
    if (fs.existsSync(findPath)) {
      this.commandList.push(`-DCMAKE_FIND_ROOT_PATH=${findPath}`);
    }
    return this;
  }

  addPackageFindScript(script: string) {
    FileUtil.checkPathLength(script);
    if (fs.existsSync(script)) {
      this.commandList.push(`-DPACKAGE_FIND_FILE=${script}`);
    }
    return this;
  }

  addBuildType(buildType: string): NativeCommandBuilder {
    this.commandList.push(`-DCMAKE_BUILD_TYPE=${buildType}`);
    return this;
  }

  addNativeSdk(nativeSdk: string): NativeCommandBuilder {
    FileUtil.checkPathLength(nativeSdk);
    this.commandList.push(`-DOHOS_SDK_NATIVE=${nativeSdk}`);
    return this;
  }

  addHMosNativeSdk(nativeSdk: string): NativeCommandBuilder {
    FileUtil.checkPathLength(nativeSdk);
    this.commandList.push(`-DHMOS_SDK_NATIVE=${nativeSdk}`);
    return this;
  }

  addSystemName(systemName: string): NativeCommandBuilder {
    this.commandList.push(`-DCMAKE_SYSTEM_NAME=${systemName}`);
    return this;
  }

  addOhosArchAbi(archAbi: string): NativeCommandBuilder {
    this.commandList.push(`-DCMAKE_OHOS_ARCH_ABI=${archAbi}`);
    return this;
  }

  exportCompileCommands(exportCommand: string): NativeCommandBuilder {
    this.commandList.push(`-DCMAKE_EXPORT_COMPILE_COMMANDS=${exportCommand}`);
    return this;
  }

  addToolChain(toolchain: string): NativeCommandBuilder {
    FileUtil.checkPathLength(toolchain);
    this.commandList.push(`-DCMAKE_TOOLCHAIN_FILE=${toolchain}`);
    return this;
  }

  addGenerator(generator: string): NativeCommandBuilder {
    this.commandList.push(`-G${generator}`);
    return this;
  }

  addMakeProgramPath(makeProgramPath: string): NativeCommandBuilder {
    FileUtil.checkPathLength(makeProgramPath);
    this.commandList.push(`-DCMAKE_MAKE_PROGRAM=${makeProgramPath}`);
    return this;
  }

  addCFlags(cFlags: string): NativeCommandBuilder {
    this.commandList.push(`-DCMAKE_C_FLAGS=${cFlags}`);
    return this;
  }

  addCxxFlags(cppFlags: string): NativeCommandBuilder {
    this.commandList.push(`-DCMAKE_CXX_FLAGS=${cppFlags}`);
    return this;
  }

  changeToDir(dir: string): NativeCommandBuilder {
    FileUtil.checkPathLength(dir);
    this.commandList.push('-C');
    this.commandList.push(dir);
    return this;
  }

  muteUnusedCliWarn(): NativeCommandBuilder {
    this.commandList.push('--no-warn-unused-cli');
    return this;
  }
}
